import load
import pickle

class company:
    def __init__(self,code,pingji):
        self.code=code
        self.pingji=pingji
        self.allJin=[]
        self.allXiao=[]

    def addJin(self,jin,isYouxiao,day):
        self.allJin.append((jin,isYouxiao,day))

    def addXiao(self,xiao,isYouxiao,day):
        self.allXiao.append((xiao,isYouxiao,day))

    def sort(self):
        self.allJin.sort(key=lambda x: x[2])
        self.allXiao.sort(key=lambda x: x[2])
        self.account={}

        for jine,stat,day in self.allJin:
            if stat:
                if not day in self.account:
                    self.account[day]=0
                self.account[day]-=jine # 进项是扣钱
        for jine,stat,day in self.allXiao:
            if stat:
                if not day in self.account:
                    self.account[day]=0
                self.account[day]+=jine # 消项是加钱

        self.account=list(self.account.items())
        self.account.sort(key=lambda x:x[0])

        return self.account

    def getZhouzhuanLv(self):
        self.x=[i[0] for i in self.account]
        self.y=[i[1] for i in self.account]

        tot = max(self.x) - min(self.x)
        t=0
        for i in self.y:
            if i>0:
                t+=1
        self.zhouzhuanLv=t/tot

        return self.x,self.y

    def caluLiushui(self):
        self.liushui=0
        self.jinSum=0
        for jine,stat,day in self.allJin:
            if stat:
                self.liushui+=abs(jine)
                self.jinSum+=abs(jine)
        self.xiaoSum=0
        for jine,stat,day in self.allXiao:
            if stat:
                self.liushui+=abs(jine)
                self.xiaoSum+=abs(jine)
        self.fuzhai=self.jinSum/self.liushui
        self.shouyi=(self.xiaoSum-self.jinSum)/self.liushui

    def caluMisDingDan(self):
        a=0.75
        b=1
        zuofeiSum=0
        for jine,stat,day in self.allXiao:
            if not stat: # 作废订单
                zuofeiSum+=jine
        fushuSum=0
        for jine,stat,day in self.allXiao:
            if jine<0: # 负数订单
                fushuSum-=jine
        self.misDingDan=(a*zuofeiSum+b*fushuSum)/self.xiaoSum

    def output(self):
        print(self.code+','+str(self.liushui)+','+str(self.fuzhai)+','+str(self.shouyi)+','+str(self.misDingDan),','+str(self.zhouzhuanLv))

if __name__=='__main__':
    allCompany={}

    for i in range(1,303):
        code,pingji=load.getPingji(i)
        allCompany[code] = company(code,pingji)

    for i in range(1,395176):
        code=load.getCell(i,load.code,True)
        jin=float(load.getCell(i,load.jine,True))
        day = float(load.getCell(i, load.date, True))
        allCompany[code].addJin(jin, load.getStat(i,True), day)

    for i in range(1,330836):
        code=load.getCell(i,load.code,False)
        jin=float(load.getCell(i,load.jine,False))
        day = float(load.getCell(i, load.date, False))
        allCompany[code].addXiao(jin, load.getStat(i, False), day)

    import matplotlib.pyplot as plt
    print('企业代号,总流水,负债率,收益率,问题订单率,周转率')
    for name,comp in allCompany.items():
        comp.caluLiushui()
        comp.caluMisDingDan()
        comp.sort()

        x, y = comp.getZhouzhuanLv()
        if not max(y) > 10 ** 8:
            plt.plot(x, y, label=name)

        comp.output()

    f=open('allcomp.pkl','wb')
    pickle.dump(allCompany,f)
    # plt.legend()
    # plt.show()